package com.duodao.login.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.duodao.login.model.Page;
import com.duodao.login.model.User;
import com.duodao.login.service.UserService;



//登陆控制模块
@Controller
@RequestMapping("/loginController")
public class UserController {

	@Autowired
	private UserService userService;

	// 登陆
	@RequestMapping("/login.do")
	@ResponseBody
	public String login(User user, HttpServletRequest request) {
		String pwd = null;
		String result = null;

		try {
			if (user.getAccountNo() != null)
				pwd = userService.login(user.getAccountNo());
			if (pwd != null && pwd.equals(user.getPassword())) {
				result = "login...";
				HttpSession session = request.getSession();
				session.setAttribute("ACCOUNTNO", user.getAccountNo());
			} else {
				throw new Exception("用户名或密码错误");
			}

		} catch (Exception e) {
			result = e.getMessage();

		}
		return result;
	}
	
	//退出系统
	@RequestMapping("/loginout")
	public String logon(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String loginPath = request.getContextPath()+"/page/login.jsp";
		HttpSession session = request.getSession();
		session.setAttribute("ACCOUNTNO", null);
	//	response.getWriter().print("<script>alert('退出成功')</script>");
		return "redirect:"+loginPath;
	}

	// 登陆成功跳转模块
	@RequestMapping("/toMainPage.do")
	public String findAllUser(HttpServletRequest request) {
		String accNo = null;
		HttpSession session = request.getSession();
		if (session.getAttribute("ACCOUNTNO") != null)
			accNo = (String) session.getAttribute("ACCOUNTNO");
		if (accNo == null)
			return "/page/login";
		return "/page/main";
	}
	
	//用户列表模块
	@RequestMapping(value="showUsers.do")
	@ResponseBody
	public Page showUsers(HttpServletRequest request,User user) throws Exception{
		//user=new User();
		Page page = new Page();
		List<User> rows =new ArrayList<User>();
		Long total = null;
		Integer totalRows=userService.loadUserCount(user);
		String p=request.getParameter("page");
		String r=request.getParameter("rows");
		System.out.println(p+"--"+r);
		int start=0;
		int end = 2;
		if (p!=null) {
			 start = (Integer.parseInt(p)-1)*Integer.parseInt(r);
		}if(r!=null){
			end= Integer.parseInt(p)*Integer.parseInt(r);
		}
		System.out.println(start+"-"+end);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", start);
		map.put("end", end);
		if(user!=null){
			map.put("accountNo", user.getAccountNo());
			map.put("name", user.getName());
		}
		
		List<User> user_list=null;
		try{
			user_list=userService.selectAllUser(map);

			if(user_list==null)
				throw new Exception("execute exception");
			else{				

				total=totalRows.longValue();
				rows=user_list;

			}
			
		}catch (Exception e) {
			e.getStackTrace();
		}
		page.setRows(rows);
		page.setTotal(total);
		return page;
	}
	
	@RequestMapping("checkAccountNo.do")
	@ResponseBody
	public String checkAccountNo(User user) throws Exception{
		String message=null;
		try {
			message="可用";
			if(user.getAccountNo()!=null){				
				user=userService.findByAccountNo(user.getAccountNo());
				if(user!=null){
					message="账号已存在，请更改！";
				}
			}
		} catch (Exception e) {
			message = e.getMessage();
		}
		return message;
	}
	
	@RequestMapping("saveUser.do")
	@ResponseBody
	public String saveUser(User user)throws Exception{
		String message=null;
		try {
			if(user==null)
				throw new Exception("添加用户失败！请重试");
			else{
				userService.saveUser(user);
			}
			 message="添加用户成功！";
			System.out.println(message);
		} catch (Exception e) {
			message = e.getMessage();
			System.out.println("ErrorRes:"+message);
		}		
		return message;
	}
	
	@RequestMapping("removeUser.do")
	@ResponseBody
	public String removeUser(@ModelAttribute User user) throws Exception{
		String message=null;
		try {
			if(user.getUserID()==null)
				throw new Exception("请选择删除对象！");
			else{
				userService.removeUser(user.getUserID());
				message="删除用户成功！";
			}
			System.out.println(message);
		} catch (Exception e) {
			message=e.getMessage();
			System.out.println("ErrorRes:"+message);
		}
		return message;
	}
	
	@RequestMapping("modifyUser.do")
	@ResponseBody
	public String modifyUser(User user) throws Exception{
		String message=null;
		try {
			if(user.getUserID()==null)
				throw new Exception("请选择修改对象！");
			else{
				userService.modifyUser(user);
				message="修改用户成功！";
			}
		} catch (Exception e) {
			message = e.getMessage();
		}
		return message;
	}
	
}
